using System;
using System.Threading;
using System.Threading.Tasks;
using GamePlayerTest;
using Google.Protobuf;
using Unity.VisualScripting;
using UnityEngine;
using UnityTimer;

public class MainNet : MonoBehaviour
{

    public GameObject TextField;
    public GameObject Button;
    void Start()
    {
        if(NetMgr.Instance == null)
        {
            GameObject obj = new GameObject("Net");
            obj.AddComponent<NetAsyncMgr>();
        }

        NetAsyncMgr.Instance.Connect("127.0.0.1", 8080);

        
        // if(NetMgr.Instance == null)
        // {
        //     GameObject obj = new GameObject("Net");
        //     obj.AddComponent<NetMgr>();
        // }

        // NetMgr.Instance.Connect("127.0.0.1", 8080);

        // TestOOXX();
        // UnityTimer.Timer.Register(5f, () => Debug.Log("Hello World"));

        
    }

    private async void TestOOXX()
    {
        await Task.Delay(5000);
    }



    public void OnSendMsgClick()
    {
        TestMsg testMsg = new TestMsg();
        testMsg.TestF = 0.1f;
        testMsg.TestD = 123.0;
        int cmdId = 1001;

        int bytesLen = testMsg.ToByteArray().Length;
        byte[] bytes = new byte[4 + 4 + bytesLen];

        int index = 0;
        BitConverter.GetBytes(cmdId).CopyTo(bytes, index);
        index += sizeof(int);

        BitConverter.GetBytes(bytesLen).CopyTo(bytes, index);
        index += sizeof(int);

        testMsg.ToByteArray().CopyTo(bytes, index);

        NetAsyncMgr.Instance.SendTest(bytes);

        // TestMsg.Parser.ParseFrom([]);
        
    }


    void OnDestroy()
    {
        print("OnDestroy==============");
    }


    // Update is called once per frame
    void Update()
    {
        
    }
}
